/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:assign-tracer
 * Create Time: 2016年1月22日 下午5:21:33
 */
package com.lenovo.lmrp.server.assigntracer.util.pusher;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.lenovo.sdc.framework.sns.ASMSResponse;
import com.lenovo.sdc.framework.util.exception.ErrorCode;
import com.lenovo.sdc.framework.util.exception.TraceableException;

/**
 * @author songyz
 *
 */
public class SMSResponseImpl extends ASMSResponse {
    private Map<Object, Object> obj;
    private String resultCode;
    private int sendNumber;
    private String stateMessage;
//    private SMSResponseObj responseObj = new SMSResponseObj();

//    public class SMSResponseObj {
//        
//        private String resultCode;
//        private int sendNumber;
//        private String stateMessage;
//    }

    @Override
    public SMSResponseImpl getBody() {
        @SuppressWarnings("unchecked")
        HashMap<Object, Object> object = (HashMap<Object, Object>) ((HashMap<Object, Object>) ((HashMap<Object, Object>) obj
            .get("Body")).get("SendMessageResponse")).get("SendMessageResult");
        this.resultCode = (String) object.get("Resultcode");
        this.sendNumber = Integer.parseInt((String) object.get("SendNumber"));
        this.stateMessage = (String) object.get("StatusMessage");
        //解析生成一些关于响应的类，以供MQ调用
        return this;
    }

    @Override
    public void parse(String body) {
        //解析相应的xml，系统中现成的Object
        ObjectMapper mapper = new ObjectMapper(new XmlFactory());
        mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        try {
            @SuppressWarnings("unchecked")
            Map<Object, Object> obj = mapper.readValue(body, Map.class);
            this.obj = obj;
        }
        catch (Exception e) {
            throw new TraceableException(ErrorCode.COMM_PARSE_FAILED, e);
        }
    }

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public int getSendNumber() {
        return sendNumber;
    }

    public void setSendNumber(int sendNumber) {
        this.sendNumber = sendNumber;
    }

    public String getStateMessage() {
        return stateMessage;
    }

    public void setStateMessage(String stateMessage) {
        this.stateMessage = stateMessage;
    }
}
